/**
 * Copyright (c) 2020 Coder League
 * All rights reserved.
 *
 * File：SystemLogDaoImpl.java
 * History:
 *         2020年12月19日: Initially created, Chrise.
 */
package club.coderleague.cfss.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import club.coderleague.cfss.dao.extension.SystemLogDaoExtension;
import club.coderleague.cfss.domain.beans.PagingParameters;
import club.coderleague.cfss.domain.beans.SystemLogsExtension;
import club.coderleague.cfss.domain.entities.Systemlogs;
import club.coderleague.cfss.domain.enums.EntityState;
import club.coderleague.data.jpa.domain.Page;
import club.coderleague.data.jpa.id.support.SnowflakeV4;
import club.coderleague.data.jpa.repository.support.AbstractDataRepositoryExtension;

/**
 * 系统日志数据访问对象实现。
 * @author Chrise
 */
public class SystemLogDaoImpl extends AbstractDataRepositoryExtension<Systemlogs, Long, SnowflakeV4> implements SystemLogDaoExtension {
	/**
	 * @see club.coderleague.cfss.dao.extension.SystemLogDaoExtension#findPageByParamsMap(club.coderleague.cfss.domain.beans.PagingParameters, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Page<SystemLogsExtension> findPageByParamsMap(PagingParameters pagingparameters, String keyword, String starttime, String endtime) {
		StringBuilder qsqlBuilder = new StringBuilder("select sl.entityid as entityid, sl.creator as creator, sl.createtime as createtime,"
			+ " sl.modifier as modifier, sl.modifytime as modifytime, sl.entitystate as entitystate, sl.operator as operator, sl.operatortype as operatortype,"
			+ " sl.operatetime as operatetime, sl.operateaddr as operateaddr, sl.operatedesc as operatedesc, sl.requesttype as requesttype,"
			+ " sl.returnresult as returnresult, u.username as operatorusername"
			+ " from systemlogs sl"
			+ " left join users u on u.entityid = sl.operator"
			+ " where sl.entitystate = :validstate");
		StringBuilder csqlBuidler = new StringBuilder("select count(*) from systemlogs sl where sl.entitystate = :validstate");
		Map<String, Object> params = new HashMap<>();
		params.put("validstate", EntityState.VALID.getValue());
		// 开始时间
		if (StringUtils.isNotBlank(starttime)) {
			qsqlBuilder.append(" and DATEDIFF(sl.operatetime, :starttime) >= 0");
			csqlBuidler.append(" and DATEDIFF(sl.operatetime, :starttime) >= 0");
			params.put("starttime", starttime);
		}
		// 结束时间
		if (StringUtils.isNotBlank(endtime)) {
			qsqlBuilder.append(" and DATEDIFF(sl.operatetime, :endtime) <= 0");
			csqlBuidler.append(" and DATEDIFF(sl.operatetime, :endtime) <= 0");
			params.put("endtime", endtime);
		}
		// 关键字
		if (StringUtils.isNotBlank(keyword)) {
			qsqlBuilder.append(" and (sl.operatedesc like :keyword or sl.operateaddr like :keyword)");
			csqlBuidler.append(" and (sl.operatedesc like :keyword or sl.operateaddr like :keyword)");
			params.put("keyword", "%" + keyword + "%");
		}
		// 排序
		qsqlBuilder.append(" order by ");
		if (StringUtils.isNoneBlank(pagingparameters.getSortField(), pagingparameters.getSortOrder())) {
			qsqlBuilder.append(pagingparameters.getSortField()).append(" ").append(pagingparameters.getSortOrder());
		} else {
			qsqlBuilder.append("createtime desc");
		}
		return super.queryCustomBeanPageBySql(SystemLogsExtension.class, csqlBuidler.toString(), qsqlBuilder.toString(), pagingparameters.getPageIndex(), pagingparameters.getPageSize(), params);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.SystemLogDaoExtension#findExtensionByEntityid(java.lang.Long)
	 */
	@Override
	public SystemLogsExtension findExtensionByEntityid(Long entityid) {
		String sql = "select sl.entityid as entityid, sl.creator as creator, sl.createtime as createtime,"
			+ " sl.modifier as modifier, sl.modifytime as modifytime, sl.entitystate as entitystate, sl.operator as operator, sl.operatortype as operatortype,"
			+ " sl.operatetime as operatetime, sl.operateaddr as operateaddr, sl.operatedesc as operatedesc, sl.requesttype as requesttype,"
			+ " sl.returnresult as returnresult, sl.invokemethod as invokemethod, sl.requestdata as requestdata, sl.responsedata as responsedata,"
			+ " u.username as operatorusername"
			+ " from systemlogs sl"
			+ " left join users u on u.entityid = sl.operator"
			+ " where sl.entityid = ?0 and sl.entitystate = ?1";
		return super.queryCustomBeanBySql(SystemLogsExtension.class, sql, entityid, EntityState.VALID.getValue());
	}
}
